import { observable, computed } from 'mobx';
import { updateBanner, createBanner, getBanner } from './BannerHttpResource';
import * as Validator from '../../../service/Validator';
import { ItemModel, IItemProps, Validate } from '../../ItemModel';

export interface IBannerModel extends IItemProps {
    name: string;  // 名称
    url: string;  // 跳转链接
    imageUrl: string;  // 图片
    note: string;  // 备注
}

export class BannerModel extends ItemModel<IBannerModel>  {

    @observable data: IBannerModel = {
        name: "",
        url: "",
        imageUrl: "",
        note: "",
    };

    @Validate(`请输入Banner名称`)
    @computed get nameValid() { return Validator.notEmpty(this.data.name); }

    @Validate(`请输入合法的Banner跳转链接`)
    @computed get urlValid() { return Validator.isUrl(this.data.url); }

    @Validate(`请上传一张Banner图片`)
    @computed get imageUrlValid() { return Validator.notEmpty(this.data.imageUrl); }
    
    fetchItemImpl(id: string) {
        return getBanner(id);
    }

    createItemImpl(data: IBannerModel) {
        return createBanner(data);
    }

    updateItemImpl(id: string, data: IBannerModel) {
        return updateBanner(id, data);
    }
    
}